package com.sdk.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;

public class CheckableTextView extends TextView {

	private Rect mRect;
	private Paint mPaint;
	private boolean mIsChecked;
	private static final int CHECKED_TEXT_COLOR = Color.GRAY;

	public CheckableTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mRect = new Rect();
		mPaint = new Paint();
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setColor(CHECKED_TEXT_COLOR);
	}

	@Override
	protected void onDraw(Canvas canvas) {

		int count = getLineCount();
		Rect r = mRect;
		Paint paint = mPaint;
		int lineHeight = getLineHeight();
		
		if (mIsChecked) {
			for (int i = 0; i < count; i++) {
				int baseline = getLineBounds(i, r);
				canvas.drawLine(r.left, baseline - (lineHeight / 3), r.right,
						baseline - (lineHeight / 3), paint);
			}
			setTextColor(CHECKED_TEXT_COLOR);
		}
		super.onDraw(canvas);
	}

	public void setChecked(boolean isChecked) {
		mIsChecked = isChecked;
		postInvalidate();
	}

}
